using System;

namespace LogikBug
{
	/// <summary>
	/// 
	/// </summary>
	public static class EnumExtensions
	{
		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="enumType"></param>
		/// <param name="value"></param>
		/// <param name="outEnum"></param>
		/// <returns></returns>
		public static bool TryParse<T>(this Enum enumType, string value, out T outEnum)
		{
			outEnum = default(T);

			if (value.IsNotNullOrEmpty() && (typeof(T).IsAssignableFrom(typeof(Enum))))
			{
				if (Array.Find(Enum.GetNames(typeof(T)), e => e == value).IsNotNullOrEmpty())
				{
					try
					{
						outEnum = (T)Enum.Parse(typeof(T), value, true);
						return true;
					}
					catch (Exception)
					{
						return false;
					}
				}
			}

			return false;
		}
	}
}